package com.intellagric.controller.module.agriecom_produce;

import com.intellagric.agriecom.module.agriecom_produce.ProduceService;

import com.intellagric.agriecom.module.agriecom_quotation.QuotationService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomProduce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.jms.*;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Autuor cky
 * @Date 2018/11/10 15:22
*/
@Controller

public class AgriecomProduceController {

    @Autowired
    private ProduceService produceService;

    @Autowired
    private QuotationService quotationService;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination productTopic;

    /**
     * 根据id查找
     * @param id
     * @return
     */

    @RequestMapping("/agriecomProduce/{id}")
    @ResponseBody
    public ResponseMessage selectOne(@PathVariable("id") String id){
        return produceService.selectProduceById(id); }

    /**
     * 添加商品信息
     * @param produce
     * @return ResponseMessage
     */

    @RequestMapping("/agriecomProduce/add")
    @ResponseBody
    public ResponseMessage addProduce(AgriecomProduce produce){
        final String id;
        if(produce!=null) {
            produce.setProduceId(UUID.randomUUID().toString().replace("-",""));
            if(produce.getDelFlag()==null||produce.getDelFlag().equals(""))
                produce.setDelFlag("0");                           //默认正常不删除
            if(produce.getIsHot()==null||produce.getIsHot().equals(""))
                produce.setIsHot("0");                           //默认非热门
            Date date=new Date(System.currentTimeMillis());
            produce.setCreateDate(date);
            produce.setUpdateDate(date);

            ResponseMessage msg=produceService.insertProduce(produce);
            id=produce.getCategoryId();
            if(msg.getCode()==200)  //添加成功
            {
                jmsTemplate.send(productTopic, new MessageCreator() {
                    @Override
                    public Message createMessage(Session session) throws JMSException {
                        TextMessage textMessage=session.createTextMessage();
                        textMessage.setText(id);
                        return textMessage;
                    }
                });
            }
            return msg;
        }
        return ResponseMessage.fail("插入数据为空");
    }

    /**
     * 删除商品
     * @Param id
     * @return ResponseMessage
     */

    @RequestMapping("/agriecomProduce/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        //删除也要删除相应商品的行情
        quotationService.deleteQuotation(id);
        return produceService.deleteProduce(id);
    }
    /**
     * Ade
     * 批量删除产品
     * @param ids
     * @return
     */
    @RequestMapping("/agriecomProduce/batchDelete")
    @ResponseBody
    public ResponseMessage batchDeleteMerchant(@RequestParam(value="ids[]") String[] ids) {
        //批量删除也要删除相应商品的行情
        quotationService.batchDeleteQuotation(ids);
        return produceService.batchDeleteProduce(ids);
    }


    /**
     * 编辑修改新闻种类信息
     * @Param newsType
     * @return ResponseMessage
     */

    @RequestMapping("/agriecomProduce/edit")
    @ResponseBody
    public ResponseMessage edit(AgriecomProduce produce) {
        ResponseMessage responseMessage=produceService.editProduce(produce);
        if(produce.getPrice()!=produceService.queryProduceById(produce.getProduceId()).getPrice()&&responseMessage.getCode()==200){
            //调用行情增加的接口    id和price
            try {
                quotationService.priceChange(produce.getProduceId(),produce.getPrice());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return responseMessage;
    }

    // Ade添加
    //根据商品分类ID查询商品集合
    @RequestMapping("/agriecomProduce/category")
    @ResponseBody
    public LayuiDataGridResult queryByCategoryId(int page,int limit,String categoryId){
        return produceService.getListByCategoryId(page,limit,categoryId);
    }


    /**
     * 分页查询(restful) order:传入desc或者asc或空（数据库顺序）
     * @param page
     * @param limit
     * @param order
     * @return
     */

    @RequestMapping("/agriecomProduce/list")
    @ResponseBody
    public LayuiDataGridResult query(int page,int limit,String order){
        return produceService.getList(page,limit,order);
    }


    @RequestMapping("/agriecomProduce/produceList")
    @ResponseBody
    public LayuiDataGridResult queryByKeyword(int page,int limit,String keyword){
        return produceService.getproduceByKeyword(page,limit,keyword);
    }



//    /**
//     * 价格排序查询所有 传入desc或者asc或空（数据库顺序）
//     * @param order
//     * @return
//     */
//    @RequestMapping("price/{order}")
//    @ResponseBody
//    public LayuiDataGridResult queryAllAsc(@PathVariable("order") String order){
//        LayuiDataGridResult lay=new LayuiDataGridResult();
//        List<AgriecomProduce> list=produceService.getAll(order);
//        if(list==null)
//            return LayuiDataGridResult.fail();
//        lay.add(list,list.size());
//        lay.setMsg("查询成功");
//        return lay;
//    }



}
